package cn.bdqn.service.impl;

import cn.bdqn.dto.Dto;
import cn.bdqn.mapper.ItripCommentMapper;
import cn.bdqn.mapper.ItripHotelOrderMapper;
import cn.bdqn.mapper.ItripImageMapper;
import cn.bdqn.pojo.ItripComment;
import cn.bdqn.pojo.ItripImage;
import cn.bdqn.service.ItripCommentService;
import cn.bdqn.vo.ItripScoreCommentVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 2018/8/23
 */
@Service
public class ItripCommentServiceImpl implements ItripCommentService {
    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripImageMapper itripImageMapper;


    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;



    @Override
    public Dto addComment(ItripComment itripComment, List<ItripImage> itripImages) throws Exception {
        Dto dto=new Dto();

        if (null!=itripComment){
            //综合评分=(设施+卫生+位置+服务)/4
            float score=0;
            int sum=(itripComment.getFacilitiesScore()+itripComment.getServiceScore()+itripComment.getHygieneScore()+itripComment.getPositionScore());
            score=sum/4;
            //四舍五入
            itripComment.setScore(Math.round(score));
            Long commentId = 0L;
            if(itripCommentMapper.insertItripComment(itripComment)> 0 ){
                commentId = itripComment.getId();
                if(null != itripImages && itripImages.size() > 0 && commentId > 0){
                    for (ItripImage itripImage:itripImages) {
                        itripImage.setTargetId(commentId);
                        itripImageMapper.insert(itripImage);
                    }
                }
                //更新订单表-订单状态为4（已评论）
                itripHotelOrderMapper.updateHotelOrderStatus(itripComment.getOrderId(),itripComment.getCreatedBy());
                dto.setCode("0000");
                dto.setMsg("点评成功");
                dto.setSuccess("true");
            }

              /*  //判断是否有图片 如果有 添加
                if (itripComment.getIsHavingImg()==1){
                    //操作itripImageMapper 增加 imgUrl ，type
                    itripCommentMapper.insertSelective();
                }*/
            }else {
                dto.setCode("10086");
                dto.setMsg("点评失败");
                dto.setSuccess("false");
            }

        return dto;
    }
    @Override
    public List<ItripScoreCommentVO> getAvgAndTotalScore(Long hotelId) {
        return itripCommentMapper.getCommentAvgScore(hotelId);
    }

    @Override
    public Integer getItripCommentCountByMap(Map<String, Object> param) throws Exception {
        return itripCommentMapper.getItripCommentCountByMap(param);
    }

}
